//
//  WBMainViewController.swift
//  Weibo
//
//  Created by eju_wangzhenxing on 16/11/4.
//  Copyright © 2016年 qisitianxia. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        
        setUpAddBtn()
    }
    
    // portrait 竖屏，landscape 横屏
    // 代码控制 可单独处理 一个视图的支持方向
    // 设置支持的方向后 ，当前的控制器及子控制器都会遵循这个方向
    // 如果播放视频 通常是通过 model 展现的
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
 
    fileprivate lazy var addBtn = UIButton()
    
    @objc fileprivate func addBtnClick() {
        let nav = UINavigationController.init(rootViewController: UIViewController())
        
        self.present(nav, animated: true) { 
            
        }
    }
}

// 类似于 oc 的分类 ，还可以起到分隔代码的作用
// 里边不可以定义属性
extension WBMainViewController {
    
    fileprivate func setUpAddBtn() {
        addBtn.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: .normal)
        addBtn.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        addBtn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: .normal)
        addBtn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        addBtn.addTarget(self, action: #selector(addBtnClick), for: .touchUpInside)
        tabBar.addSubview(addBtn)
        
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        
        let w = tabBar.bounds.width / count - 1
        
        addBtn.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
    }
    
    //设置所以子控制器
    fileprivate func setupChildControllers() {
        
        let array = [
            ["clsName":"WBHomeViewController","title":"首页","imageName":"home"],
            ["clsName":"WBMessageViewController","title":"消息","imageName":"message_center"],
            [:],
            ["clsName":"WBDiscoverViewController","title":"发现","imageName":"discover"],
            ["clsName":"WBProfileViewController","title":"我","imageName":"profile"]
        ]
        
        var arrayM = [UIViewController]()
        
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    
    private func controller(dict: [String:String]) -> UIViewController {
        
        guard let clsName = dict["clsName"],
        let title = dict["title"],
        let imageName = dict["imageName"],
        let cls = NSClassFromString((Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "") + "." + clsName) as? UIViewController.Type
        else {
            
            return UIViewController()
        }
        
        let vc = cls.init()
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage.init(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage.init(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        // 设置tabbar 标题颜色
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.lightGray,NSFontAttributeName:UIFont.systemFont(ofSize: 10)], for: .normal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        // 实例化导航控制器时 添加rootVc时候 会默认做一次 push 操作
        let nav = WBNavigationViewController.init(rootViewController: vc)
        
        return nav
    }
}
